package org.cttic.visualization.visualization.util;

import org.cttic.visualization.visualization.common.sectionBillQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @create 2020-05-25 9:27
 */
@Component
public class queryBillMileUtil {
    @Autowired
    JdbcTemplate jdbcTemplate;
    public  void queryBillMile(String tollIntervals,sectionBillQuery sectionBillQuery){
        if(!tollIntervals.contains("|")){
            String sql2 ="SELECT unitId,provinceId,unitLength FROM `T_CHARGING_UNIT` where  unitId= '"+tollIntervals+"'";
            List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql2);
            if(maps!=null&&maps.size()>0){
                Map<String, Object> map = maps.get(0);
                Object unitLength = map.get("unitLength");
                float value = Float.parseFloat(String.valueOf(unitLength)) / 1000;//单位m 需转km
                System.out.println("里程数："+value);
                sectionBillQuery.setBillMile(value);
            }

        }else {
            Float BillAm=0.00f;
            String[] split = tollIntervals.split("\\|");
            for (String s : split) {
                String sql3 ="SELECT unitId,provinceId,unitLength FROM `T_CHARGING_UNIT` where  unitId= '"+s+"'";
                List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql3);
                if(maps!=null&&maps.size()>0){
                    Map<String, Object> map = maps.get(0);
                    Object unitLength = map.get("unitLength");
                    BillAm+=Float.parseFloat(String.valueOf(unitLength))/1000;//单位m 需转km

                }
            }
            System.out.println("里程数："+BillAm);
            sectionBillQuery.setBillMile(BillAm);
        }
    }
}
